package com.lalifa.googledate.ui.activity.login

import android.annotation.SuppressLint
import android.content.Intent
import com.facebook.CallbackManager
import com.facebook.FacebookCallback
import com.facebook.FacebookException
import com.facebook.login.LoginManager
import com.facebook.login.LoginResult
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.api.ApiException
import com.google.android.gms.tasks.Task
import com.lalifa.base.BaseActivity
import com.lalifa.extension.isEmail
import com.lalifa.extension.onClick
import com.lalifa.extension.start
import com.lalifa.extension.text
import com.lalifa.extension.toast
import com.lalifa.googledate.R
import com.lalifa.googledate.api.UserBean
import com.lalifa.googledate.databinding.ActivityLoginBinding
import com.lalifa.googledate.ui.MApplication
import com.lalifa.googledate.ui.activity.MainActivity
import com.lalifa.googledate.util.Tool
import com.lalifa.googledate.util.UserManager


@Suppress("DEPRECATED_IDENTITY_EQUALS")
class LoginActivity : BaseActivity<ActivityLoginBinding>() {
    override fun getViewBinding() = ActivityLoginBinding.inflate(layoutInflater)

    var callback: CallbackManager? = null

    override fun initView() {
        //Facebook登录
        callback = CallbackManager.Factory.create()
        LoginManager.getInstance().registerCallback(callback,
            object : FacebookCallback<LoginResult> {
                override fun onSuccess(loginResult: LoginResult) {

                }

                override fun onCancel() {
                    // App code

                }

                override fun onError(exception: FacebookException) {
                    // App code

                }
            })
    }

    override fun onStart() {
        super.onStart()
        //检查是否有登录用户
        val account = GoogleSignIn.getLastSignedInAccount(this)
        updateUI(account)
    }

    @SuppressLint("RestrictedApi")
    override fun onClick() {
        super.onClick()
        binding.apply {
            other1.onClick {
                signIn()
            }
            other2.onClick {
                faceLogin()
            }
            register.onClick {
                start(RegisterActivity::class.java)
            }
            forget.onClick {
                start(ForgetActivity::class.java)
            }
            login.onClick {
                val email = etEmail.text()
                val pass = etPass.text()
                if (email.isEmpty()) {
                    toast(getString(R.string.place_input_email))
                    return@onClick
                }
                if (!email.isEmail()) {
                    toast(getString(R.string.email_error))
                    return@onClick
                }
                if (pass.isEmpty()) {
                    toast(getString(R.string.place_input_pass))
                    return@onClick
                }


//                scopeNetLife {
//                    val user = loginApi(email, pass)
//                    if (user.code == 1) {
//                        InitNet.initNetHttp(
//                            this@LoginActivity,
//                            user.data!!.userinfo.token.pk()
//                        )
//                        UserManager.save(user.data!!.userinfo)
//                        ActivityManager.getInstance().finishAllActivities()
//                        start(MainActivity::class.java)
//                        finish()
//                    }
//                }
            }
        }
    }

    //facebook
    private fun faceLogin() {
        LoginManager.getInstance().logInWithReadPermissions(
            this, listOf("public_profile")
        )
    }

    //以下为谷歌
    private fun signIn() {
        val signInIntent: Intent = MApplication.get().mGoogle!!.signInIntent
        startActivityForResult(signInIntent, Tool.RESULTCODE_SUCCESS)
    }

    @Deprecated("Deprecated in Java")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        callback!!.onActivityResult(requestCode, resultCode, data)
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode === Tool.RESULTCODE_SUCCESS) {
            //谷歌登录回调
            val task: Task<GoogleSignInAccount> =
                GoogleSignIn.getSignedInAccountFromIntent(data)
            handleSignInResult(task)
        }
    }

    //GoogleSignInAccount 对象包含已登录用户的相关信息，例如用户的姓名。
    private fun handleSignInResult(completedTask: Task<GoogleSignInAccount>) {
        try {
            val account = completedTask.getResult(ApiException::class.java)
            updateUI(account)
        } catch (e: ApiException) {
            // The ApiException status code indicates the detailed failure reason.
            // Please refer to the GoogleSignInStatusCodes class reference for more information.
            updateUI(null)
        }
    }


    private fun updateUI(account: GoogleSignInAccount?) {
        if (account != null) {
            start(MainActivity::class.java)
            finish()
        }
    }

}